<template>
  <AppPage class="bg-[#f5f6fa] h-screen" article-class="" header-class="bg-[#3d8ffd]">
    <template #center>
      <span class="c-white">材料上传</span>
    </template>
    <div class="pb-70">
      <div class="px-15 py-15 mb-10">
        <div class="bg-white rounded-6 px-12 pb-12">
          <h3 class="pt-10 text-15 pb-6 font-600">请上传资料</h3>
          <p class="text-13 pb-6">为确保您的签证申请顺利通过，请上传真实有效信息！</p>
          <div class="mt-12">
            <!-- {{ uploadInfo.fileList }} -->
            <uploadFile :defaultFileList="defaultFileList" :material="uploadInfo" v-model:fileList="uploadInfo.fileList" @changeFile="changeFile" />
          </div>
          <!-- 1725039962325897216 -->
        </div>
      </div>
      <div class="px-15 mb-12" v-if="infoList.length">
        <div class="bg-white rounded-6 px-12 pb-12">
          <div class="flex justify-between items-center py-12">
            <span class="text-15 pb-6 font-600">
              上传说明
            </span>
            <btnPreviewDownLoad v-if="uploadInfo.sample" type="sample" :sample="uploadInfo.sample" />
            <btnPreviewDownLoad v-else-if="uploadInfo.template" type="template" :sample="uploadInfo.template" />
            <!-- <nut-button type="default" plain size="small" color="#3d8ffd">看样图</nut-button> -->
          </div>
          <p class="text-13 pb-6" v-if="infoList.length" v-for="(item, i) in infoList" :key="i">{{ item }}</p>
          <p class="text-13 pb-6" v-else-if="!infoList.length">暂无要求</p>
        </div>
      </div>
    </div>
    <div class="w-full px-15 box-border fixed bottom-0 left-0  bg-white py-10">
      <div class="w-full  box-border" @click="submitFile">
        <nut-button :block="true" type="info" color="#3d8ffd">提交</nut-button>
      </div>
    </div>
  </AppPage>
  <datePick ref="selectDateTime" v-model:value="basicData.travelTime" />
</template>
<script setup>
import axios from "axios";
import { onMounted, reactive, ref } from 'vue'
import { showToast } from '@nutui/nutui';
import '@nutui/nutui/dist/packages/toast/style';
import { Edit, Plus, Del, Right } from '@nutui/icons-vue';
import { getProvinceAddress, addOrder, uploadFileMaterial } from '@/api/visa'
import { useRoute, useRouter } from 'vue-router'
import datePick from '@/components/datePick/index.vue'
import positionData from '../components/positionData.vue'
import selectIdentity from '@/components/select/select.vue'
import provinceCityCounty from '@/components/provinceCityCounty/index.vue'
import uploadFile from '@/components/uploadFile/uploadFile.vue'
import { identityTYpeColumns } from '@/utils/type/visa.js'
import { sevenDayBefore } from '@/utils/common/common.js'
import { isEmptyObject } from '@/utils/common/is.js'
import { useVisaStore } from '@/store/modules/visa/index.js'
import { productDetailQuery, productBriefQuery, } from '@/api/visa'
import { visaType, identityTYpe } from '@/utils/type/visa.js'
import btnPreviewDownLoad from '@/components/btnPreviewDownLoad/index.vue';
import { getToken } from '@/utils'
import { _token } from '@/utils/auth/token'
const route = useRoute()
const router = useRouter()
const basicData = reactive({
  travelTime: '2023-12-12',
  contact_name: 'zhonhai',
  contact_number: '18126495952',
  address: ''
})
let identityMaterial = !isEmptyObject(useVisaStore().identityMaterial) ? useVisaStore().identityMaterial : (window.localStorage.getItem('identityMaterial') && JSON.parse(window.localStorage.getItem('identityMaterial')))
let infoList = ref([])
let uploadInfo = reactive({
  fileList: []
})
const changeFile = (file) => {
  // fileList.value.push(file)
  uploadInfo.fileList.push(file)
}



let defaultFileList = ref([])
// const token = ''
// 提交文件
// const uploadUrl = 'https://test1.somytrip.com/tourism_AI/api/third/visa/upload/file';
// const uploadUrl = 'https://test1.somytrip.com/tourism_AI/api/third/visa/upload/file/v2';
const submitFile = async () => {
  if (!uploadInfo.fileList.length) {
    showToast.text('请上传文件！');
    return
  }
  let fileList = []
  uploadInfo.fileList.map(item => {
    if (item && item.fileName) {
      fileList.push(item.fileName)
    }
  })

  showToast.loading('提交中', { cover: true, duration: 0 });
  let params = {
    "userId": useVisaStore().uId,
    "visaOrderId": identityMaterial.order_id || 15215226,
    "materialId": uploadInfo.material_id,
    "materialName": uploadInfo.material_name,
    "fileNames": fileList,
    "applicantSetIndex": identityMaterial.identityIndex
  }
  try {
    const { code, data, success } = await uploadFileMaterial(params)
    if (+code === 200 && success) {
      const env = import.meta.env
      identityMaterial.zip_url = env.VITE_BASE_URL + data.zip_detail_set[identityMaterial.identityIndex].zip_url
      const material = identityMaterial.material
      for (const _key in material) {
        if (Array.isArray(material[_key]) && _key === 'elec_material') {
          material[_key].forEach(_item => {
            if (+_item.material_id === +uploadInfo.material_id) {
              item.fileList = uploadInfo.fileList
            }
          })
        } else if (_key === 'application' && _key === uploadInfo.material_id) {
          material[_key].fileList = uploadInfo.fileList
        } else if (_key === 'idphoto' && _key === uploadInfo.material_id) {
          material[_key].fileList = uploadInfo.fileList
        }
      }
      useVisaStore().setIdentityMaterial(identityMaterial)
      window.localStorage.setItem('identityMaterial', JSON.stringify(identityMaterial))
      showToast.text('提交成功！');
      router.go(-1)
      showToast.hide()
    }
    showToast.hide()
  } catch (error) {
    showToast.hide()

  }
}


onMounted(async () => {
  let _uploadInfo = useVisaStore().uploadInfo
  for (const key in _uploadInfo) {
    uploadInfo[key] = _uploadInfo[key];
  }
  console.log('%c [ uploadInfo ]-144', 'font-size:13px; background:#ae5135; color:#f29579;', uploadInfo)

  let attention = uploadInfo.attention
  if (attention) {
    if (attention.includes('/n')) {
      infoList.value = attention.split('')
    } else {
      infoList.value = [attention]
    }
  }
  let material = identityMaterial.material
  for (const _key in material) {
    if (Array.isArray(material[_key]) && _key === 'elec_material' && uploadInfo.type === 'elec_material') {
      material[_key].forEach(_item => {
        if (_item.material_id == uploadInfo.material_id) {
          uploadInfo.fileList = _item.fileList
          defaultFileList.value = _item.fileList
        }
      })
    } else if (_key === uploadInfo.material_id && _key === 'application' && uploadInfo.type === 'application') {
      uploadInfo.fileList = material[_key].fileList
      uploadInfo.material_id = _key
      uploadInfo.material_name = material[_key].application_name || 'application_name' //application_name  template photo_name idphoto
      defaultFileList.value = material[_key].fileList
    } else if (_key === uploadInfo.material_id && _key === 'idphoto' && uploadInfo.type === 'idphoto') {
      uploadInfo.fileList = material[_key].fileList
      uploadInfo.material_id = _key
      uploadInfo.material_name = material[_key].photo_name || 'photo_name' //application_name  template photo_name idphoto
      defaultFileList.value = material[_key].fileList
    }
  }
})
</script>
<style scoped>
.card-tabs .n-tabs-nav--bar-type {
  padding-left: 4px;
}
</style>
<style lang="scss" scoped>
.city {
  line-height: 32px;
  border: 1px solid #ddd;
}
.city.active {
  line-height: 32px;
  border: 1px solid #f00;
}
</style>